package org.durandj.ystream.client.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;

import org.durandj.ystream.client.util.Settings;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
// TODO: Javadoc!
public class SettingsDialog extends javax.swing.JDialog
{

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private static final long serialVersionUID = 6893341759590752577L;
	
	/**
	 * The user chose to apply the current settings.
	 */
	public static final int RESULT_APPLY = 0;
	
	/**
	 * The user chose to not apply the current settings.
	 */
	public static final int RESULT_CANCEL = 1;
	
	private JLabel lblVLC;
	private JButton btnCancel;
	private JButton btnApply;
	private JButton btnVLC;
	
	/**
	 * The outcome of the dialog.
	 */
	private int result;
	
	/**
	 * The directory of VLC.
	 */
	private String vlcDirectory;

	public SettingsDialog(JFrame frame)
	{
		super(frame);
		initGUI();
		
		result = -1;
	}
	
	private void initGUI()
	{
		try
		{
			{
				this.setTitle("Settings");
				this.setModal(true);
				this.setName("Settings");
				this.setResizable(false);
				getContentPane().setLayout(null);
				{
					lblVLC = new JLabel();
					getContentPane().add(lblVLC);
					lblVLC.setText("VLC Settings");
					lblVLC.setBounds(6, 12, 71, 16);
				}
				{
					btnVLC = new JButton();
					getContentPane().add(btnVLC);
					btnVLC.setText("Edit");
					btnVLC.setBounds(83, 6, 49, 28);
					btnVLC.setToolTipText("Edit the VLC information.");
					
					btnVLC.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0)
						{
							VLCDialog dlg = new VLCDialog((JFrame)getOwner());
							dlg.setPath(Settings.getSettings().getSetting("vlc_path"));
							dlg.setVisible(true);
							
							if(dlg.getResult() == VLCDialog.RESULT_CANCEL)
								return;
							
							vlcDirectory = dlg.getPath();
						}
					});
				}
				{
					btnApply = new JButton();
					getContentPane().add(btnApply);
					btnApply.setText("Apply");
					btnApply.setBounds(358, 88, 57, 28);
					btnApply.setToolTipText("Apply all setting changes.");
					
					btnApply.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e)
						{
							result = RESULT_APPLY;
							
							setVisible(false);
						}
					});
				}
				{
					btnCancel = new JButton();
					getContentPane().add(btnCancel);
					btnCancel.setText("Cancel");
					btnCancel.setBounds(421, 88, 67, 28);
					btnCancel.setToolTipText("Close without changing anything.");
					
					btnCancel.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e)
						{
							result = RESULT_CANCEL;
							
							setVisible(false);
						}
					});
				}
			}
			this.setSize(500, 150);
			this.setLocationRelativeTo(this.getParent());
			
			// We need to know some other things about the window so lets listen in.
			addWindowListener(new WindowAdapter() {
				@Override
				public void windowClosing(WindowEvent e)
				{
					result = RESULT_CANCEL;
				}
			});
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * Get the outcome of the window. This could be <code>RESULT_APPLY</code>, <code>RESULT_CANCEL</code>, or -1 if the window has not closed yet.
	 * 
	 * @return the dialog result
	 */
	public int getResult()
	{
		return result;
	}
	
	/**
	 * Gets the directory of VLC.
	 * 
	 * @return the VLC directory
	 */
	public String getVLCDirectory()
	{
		return vlcDirectory;
	}
}
